#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2010-4-21

@author: chenheng

邮件推广
'''
import smtplib
import email
import time
import os
from email.Message import Message
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

class EmailAD(object):
    smtpserver = "smtp.163.com"
    smtpuser = "chenh0624@163.com"
    smtppassword = "c0624h"
    #smtpserver = "mail.vogins.com"
    #smtpuser = "hengchen@vogins.com"
    #smtppassword = "c0624h"
    #smtpserver = "127.0.0.1"
    #smtpuser = "taokemen@taokemen.cn"
    #smtppassword = "chen0624heng"
    smtpport = "25"
    qqstartpoint = "100000000"
    qqendpoint = "100001000"
    logger_path = "/home/chenheng/workspace/python/taobao/logs/"
    logger_file = None
    TIMEFORMAT = "%Y-%m-%d~%X"    
    DATEFORMAT = "%Y-%m-%d"    
    def __init__(self): 
        if not os.path.exists(self.logger_path):
            os.makedirs(self.logger_path)
        self.logger_file = self.logger_path + "email_ad_" + time.strftime(self.DATEFORMAT,time.localtime()) + ".log"
 

    def getQQPoint(self):
        qqstartpoint = input("请输入九位QQ号码（初始）")
        qqendpoint = input("请输入终止九位QQ号码（终止）")
        if qqstartpoint > qqendpoint :
            temp = qqstartpoint 
            qqstartpoint = qqendpoint 
            qqendpoint = temp 
            if qqendpoint - qqstartpoint > 10000 :
                qqendpoint = qqstartpoint + 10000
        self.qqstartpoint = qqstartpoint
        self.qqendpoint = qqendpoint
        
    def connect(self):
        server = smtplib.SMTP(self.smtpserver,self.smtpport)
        #server.set_debuglevel(1)
        server.ehlo()
        server.login(self.smtpuser, self.smtppassword)
        return server
    
    
    def sendMultMessage(self):
        qqs = xrange(self.qqstartpoint,self.qqendpoint) 
        fp = open(self.logger_file,"a")
        info = "[" + time.strftime(self.TIMEFORMAT,time.localtime()) +"]\t" + "Start\t" + str(self.qqstartpoint) + "@qq.com\n"	
        fp.writelines(info)
        fp.close()
        for qq in qqs :
            qqemail = str(qq) + "@qq.com"
            qqemailzone = str(qq) + "@zone.qq.com"
            msgMult = MIMEMultipart("related")
            msgMult.set_charset("UTF-8")
            msgMult["Subject"] = "淘客门最新暴光！淘我所淘，爱我所爱"
            msgMult["From"] = "taokemen@taokemen.cn"
            msgMult["To"] = qqemail
            msgMult["To"] = qqemailzone 
            msgAlternative = MIMEMultipart("alternative")
            msgMult.attach(msgAlternative)
            msgText = MIMEText("<p><a href='http://taokemen.appspot.com'>淘我所淘，爱我所爱，即日起，凡在<font color='red' size='18'>淘客门</font>购买淘宝商品可获得更多优惠，更多惊喜！<font color='red' size='24'>所有数据均来自淘宝，值得信赖！</font></a></p><p><a href='http://taokemen.appspot.com'><img src='http://taokemen.appspot.com/girl.jpeg'></img></a></p>","html")
            msgAlternative.attach(msgText)
            try :
                server = emailAD.connect()
                server.sendmail(self.smtpuser,qqemail,msgMult.as_string())
                server.quit()
            except Exception , ex :
                print Exception , ex
                fp = open(self.logger_file,"a")
                error = "[" + time.strftime(self.TIMEFORMAT,time.localtime()) +"]\t" + "Failed\t" + qqemail + "\n"	
                fp.writelines(error)
                fp.close()
                print "Error -- send email to " + qqemail + " failed"
            else :
                print "Good -- send email to " + qqemail + " succeed"    
            time.sleep(60)  
        fp = open(self.logger_file,"a")
        info = "[" + time.strftime(self.TIMEFORMAT,time.localtime()) +"]\t" + "Stop\t" + str(self.qqendpoint) + "@qq.com\n"	
        fp.writelines(info)
        fp.close()
    
if __name__ == '__main__':
    emailAD = EmailAD()
    emailAD.getQQPoint()
    emailAD.sendMultMessage()
    
    
    
    
